﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.Utils;
using Jn698._45Tool.Service;
using log4net;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class CONNECTControlViewModel:ViewModelBase
    {
        public static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private GWCmdMakeUpService gWCmdMakeUpService=new GWCmdMakeUpService();
        private GWCmdMakeUpPlanService gWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
        private ObjectIDService objectIDService = new ObjectIDService();
        public static GWCmdMakeUp GWCmdMakeUp = null;
        public static GWCmdMakeUpPlan GWCmdMakeUpPlan = null;
        public ObjectID objectID =null;
        private string version;

        public string Version
        {
            get { return version; }
            set 
            { 
                Set<string>(ref version, value);
                saveData(); 
            }
        }
        private string password;

        public string Password
        {
            get { return password; }
            set { Set(ref password, value); }
        }


        private string agreement;

        public string Agreement
        {
            get { return agreement; }
            set {
                if (Set(ref agreement,value))
                {
                    saveData();
                }
               
            }
        }

        private string func;

        public string Func
        {
            get { return func; }
            set
            {
                if (Set(ref func, value))
                {
                    saveData();
                }

            }
        }

        private string sendMaxSize;

        public string SendMaxSize
        {
            get { return sendMaxSize; }
            set
            {
                if (Set(ref sendMaxSize, value))
                {
                    saveData();
                }

            }
        }

        private string recvMaxSize;

        public string RecvMaxSize
        {
            get { return recvMaxSize; }
            set
            {
                if (Set(ref recvMaxSize, value))
                {
                    saveData();
                }

            }
        }

        private string recvFromMaxSize;

        public string RecvFromMaxSize
        {
            get { return recvFromMaxSize; }
            set
            {
                if (Set(ref recvFromMaxSize, value))
                {
                    saveData();
                }

            }
        }

        private string maxAPDU;

        public string MaxAPDU
        {
            get { return maxAPDU; }
            set
            {
                if (Set(ref maxAPDU, value))
                {
                    saveData();
                }

            }
        }

        private string connectTimeout;

        public string ConnectTimeout
        {
            get { return connectTimeout; }
            set
            {
                if (Set(ref connectTimeout, value))
                {
                    saveData();
                }

            }
        }

        private int requstObj;

        public int RequstObj
        {
            get { return requstObj; }
            set
            {
                if (Set(ref requstObj, value))
                {
                    saveData();
                }

            }
        }

        private ObservableCollection<string> requstObjList;

        public ObservableCollection<string> RequstObjList
        {
            get { return requstObjList; }
            set { Set(ref requstObjList,value); }
        }


        private bool connectVisibility;

        public bool ConnectVisibility
        {
            get { 
                return connectVisibility; 
            }
            set { connectVisibility = value; RaisePropertyChanged(); }
        }

        public static Action<bool> action;
       
        public CONNECTControlViewModel()
        {
            
            RequstObjList = new ObservableCollection<string> {"公共连接","一般密码","对称加密","数字签名" };
            //action = SetB;
            //Password = "123456";
        }

        public void UpdateData()
        {
            LogHelper.WriteInfo(typeof(CONNECTControlViewModel), "应用连接");
            string[] strings;
            if (MainViewModel.TopIndex == 0)
            {
                //objectID = objectIDService.Select(2);
                //GWCmdMakeUp = gWCmdMakeUpService.Select(objectID)[0];
                strings = GWCmdMakeUp?.DataValue.Split(';');
            }
            else
            {
                //GWCmdMakeUpPlan = gWCmdMakeUpPlanService.Select("ApplicationLink")[0];
                strings = GWCmdMakeUpPlan?.DataValue.Split(';');
            }
            if (strings==null||strings.Length<8)
            {
                return;
            }
            Version = strings[0];
            Agreement = strings[1];
            Func = strings[2];
            SendMaxSize = strings[3];
            RecvMaxSize = strings[4];
            RecvFromMaxSize = strings[5];
            MaxAPDU = strings[6];
            ConnectTimeout = strings[7];
            RequstObj = Convert.ToUInt16(strings[8]);
            /*Messenger.Default.Register<bool>(this, "ConnectVisibility", (b) => { ConnectVisibility = b; });
            action = ((b) => {

                ConnectVisibility = true;
            });*/
        }

        private void saveData()
        {
            if (MainViewModel.TopIndex == 0)
            {
                if (GWCmdMakeUp == null)
                {
                    return;
                }
            }
            else
            {
                if (GWCmdMakeUpPlan == null)
                {
                    return;
                }
            }
           
            string value = "";
            value += Version;
            value += ";";
            value += Agreement;
            value += ";";
            value += Func;
            value += ";";
            value += SendMaxSize;
            value += ";";
            value += RecvMaxSize;
            value += ";";
            value += RecvFromMaxSize;
            value += ";";
            value += MaxAPDU;
            value += ";";
            value += ConnectTimeout;
            value += ";";
            value += RequstObj;
            value += ";";
            if (MainViewModel.TopIndex == 0)
            {
                GWCmdMakeUp.DataValue = value;
                gWCmdMakeUpService.Update(GWCmdMakeUp);
            }
            else
            {
                GWCmdMakeUpPlan.DataValue = value;
                gWCmdMakeUpPlanService.Update(GWCmdMakeUpPlan);
            }
                
        }
    }
}
